package projet1;

import java.io.*;
import javax.swing.JOptionPane;
/**
 * 
 * test pour voir si ça fonctionne
 */
public class Projet1
{
    private static NodeStack<Object> stack;
    public static File outFile;

    public static void main(String[] args)
    {
        stack = new NodeStack();

        try
        {
            outFile = new File("out.txt");
            if (outFile.exists())
            {
                Utils.writeTextInFile(outFile, "", true);
            }
            else
            {
                outFile.createNewFile();
            }

            String text = Utils.getTextInFile(new File(JOptionPane.showInputDialog("Entrez le fichier (Essayez \"exempleICampus.txt\" pour tester)", "exempleICampus.txt")));

            String[] lignes = text.split("\n");
            for (String ligne : lignes)
            {
                String[] essai = ligne.split(" ");
                int len = essai.length;

                for (int i = 0; i < len; i++)
                {
                    System.out.println("test : "+essai[i]);
                    System.out.println("stack : "+stack.toString());
                    if (isFloat(essai[i]) == true)
                    {
                        float valeur = Float.parseFloat(essai[i]);
                        stack.push(valeur);
                    }
                    else if (essai[i].equals("true") || essai[i].equals("false"))
                    {
                        stack.push(essai[i].compareTo("true"));
                    }
                    else if (essai[i].equals("add"))
                    {
                        Operators.add(stack);
                    }
                    else if (essai[i].equals("sub"))
                    {
                        Operators.sub(stack);
                    }
                    else if (essai[i].equals("mul"))
                    {
                        Operators.mult(stack);
                    }
                    else if (essai[i].equals("div"))
                    {
                        Operators.div(stack);
                    }
                    else if (essai[i].equals("eq"))
                    {
                        Operators.eq(stack);
                    }
                    else if (essai[i].equals("ne"))
                    {
                        Operators.ne(stack);
                    }
                    else if (essai[i].equals("dup"))
                    {
                        Operators.dup(stack);
                    }
                    else if (essai[i].equals("exch"))
                    {
                        Operators.exch(stack);
                    }
                    else if (essai[i].equals("pop"))
                    {
                        stack.pop();
                    }
                    else if (essai[i].equals("pstack"))
                    {
                        Operators.pstack(stack);
                    }
                    else if (essai[i].equals("def"))
                    {
                        Operators.def(stack);
                    }
                    else if (essai[i].charAt(0) == '/')
                    {
                        stack.push(essai[i]);
                    }
                    else if (Symboles.getValue(essai[i]) != null)
                    {
                        stack.push(Symboles.getValue(essai[i]));
                    }
                    else
                    {
                        throw new AssertionError("Unsupported operation : " + essai[i]);
                    }
                }
            }
        }
        catch (EmptyStackException e)
        {
            e.printStackTrace();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }

    public static boolean isFloat(String data)
    {
        return data.matches("^[-+]?[0-9]*\\.?[0-9]+([eE][-+]?[0-9]+)?$");
    }
}